#!/bin/bash

echo "build_51proxy.sh Start."

nginx_ins_dir="/usr/local/51proxy"
cwd=`pwd`

_exit()
{
    cd $cwd
    rm -rf nginx-1.2.0
    echo "build_51proxy.sh End."
    exit 0
}

echo "installing required package using apt-get..."
apt-get install libpcre3-dev libpcre3 gcc make zlib1g-dev libssl-dev -y &> /dev/null || _exit
echo "installing required package using apt-get, Done."

echo "extracting nginx and it's plugins..."
tar xzvf nginx-1.2.0.tar.gz                    &> /dev/null        # nginx 
echo "extracting nginx and it's plugins, Done."

cd nginx-1.2.0

echo "configuring nginx..."
./configure --user=root --group=root --with-http_ssl_module --prefix=${nginx_ins_dir} &> /dev/null || _exit
echo "configuring nginx, Done"

echo "making nginx..."
make &> /dev/null || _exit
echo "making nginx, Done"

echo "making install nginx..."
make install &> /dev/null || _exit
echo "making install nginx, Done"
cd ..

echo "setting nginx..."
cp nginx.conf ${nginx_ins_dir}/conf
cp watchdog.sh ${nginx_ins_dir}/
mv ${nginx_ins_dir}/sbin/nginx ${nginx_ins_dir}/sbin/51proxy
echo "setting nginx, Done"

# check cron
echo "setting cron..."
APP_WATCHDOG=/usr/local/51proxy/watchdog.sh
CRON_CONF="0-59/1 * * * * root	${APP_WATCHDOG}"
CRON_LINE=`grep "${APP_WATCHDOG}" /etc/crontab`
if [ "$CRON_LINE" == "" ]; then
    echo "$CRON_CONF" >> /etc/crontab
fi
echo "setting cron, Done"

_exit